/**
 * 
 */
package com.wubo.common.web;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;

/**
 * @author WuBo
 * @CreateDate 2012-2-17
 */
@Controller
public class WeatherAction extends BaseAction {
	private static Map<String, String> cache = new HashMap<String, String>();

	private String province;
	private String city;
	private String county;
	
	private String json;
	
	public void cacheWeather(){
		String key = getKey(province, city, county);
		if(cache.containsKey(key)){
			cache.remove(key);
		}
		long date = System.currentTimeMillis();
		cache.put(key, "{\"date\":"+date+","+json.substring(1));
		
		logger.debug("缓存天气："+key);
		
		jsonSuccess(null);
	}
	
	public void getWeather(){
		String key = getKey(province, city, county);
		if(cache.containsKey(key)){
			jsonSuccess(cache.get(key), true);
		}else{
			jsonSuccess(null);
		}
	}
	
	private String getKey(String... params){
		StringBuilder buff = new StringBuilder(params[0]!= null ? params[0] : "");
		if(params.length > 2){
			for(int i=1; i<params.length; i++){
				String param = params[i];
				if(param == null){param = "";}
				buff.append("-"+param);
			}
		}
		return buff.toString();
	}

	public static Map<String, String> getCache() {
		return cache;
	}

	public static void setCache(Map<String, String> cache) {
		WeatherAction.cache = cache;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCounty() {
		return county;
	}

	public void setCounty(String county) {
		this.county = county;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}
	
}
